package com.gosophia.conference.action.common;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.conferenceManagement.common.ws.ConferenceHandlerListWebService;
import com.gosophia.holidayManagement.ws.HolidayManagementWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("GetPageConferenceHandleAction")
public class GetPageConferenceHandleAction extends BaseAjaxAction<JobDTO> {

	@Autowired
	private ConferenceHandlerListWebService conferenceHandlerListWebService;

	@Autowired
	private HolidayManagementWebService holidayManagementWebService;

	private String remind;
	
	private String warn;
	
	private String noState;
	
	private String jobType;

	@Override
	public JobDTO getDetailData() {
		return null;
	}

	@Override
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
		
  		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId();
		if(searchType){
			if ("0".equals(remind) && "1".equals(warn) && noState == null) {
				// 提醒
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				// 预警
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				// 或
				isNull.or(p);
				propertyFilters.add(isNull);
			}
			if ("0".equals(remind) && warn == null && noState == null) {
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				propertyFilters.add(isNull);
			}
			if ("1".equals(warn) && remind == null && noState == null) {
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				propertyFilters.add(p);

			}
			page = conferenceHandlerListWebService.findConferenceHandleListByUserInfoId(userInfoId, 
					page.getPageNo(), page.getPageSize(), propertyFilters, orderConditions,jobType);
		}else{
			page = conferenceHandlerListWebService.findConferenceHandleListByUserInfoId(userInfoId,
					page.getPageNo(), page.getPageSize(), searchString, orderConditions);
		}
		if(page.getResult().size() == 0){
			super.getRequestResult().setRsltCode("1");
		}else{
			super.getRequestResult().setRsltCode("0");
		}
		return SUCCESS;
	}

	public void setJobType(String jobType) {
		this.jobType = jobType;
	}

	public String getJobType() {
		return jobType;
	}

	public String getRemind() {
		return remind;
	}

	public void setRemind(String remind) {
		this.remind = remind;
	}

	public String getWarn() {
		return warn;
	}

	public void setWarn(String warn) {
		this.warn = warn;
	}

	public String getNoState() {
		return noState;
	}

	public void setNoState(String noState) {
		this.noState = noState;
	}
	
	
}
